<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>判断矩阵是否是一个 X 矩阵</title>
</head>
<body>
  <script>
    function fn(grid) {
      const n = grid.length;
      for (let i = 0; i < n; ++i) {
        for (let j = 0; j < n; ++j) {
          if (i === j || (i + j) === (n - 1)) {
            if (grid[i][j] === 0) {
              return false;
            }
          } else if (grid[i][j] !== 0) {
            return false;
          }
        }
      }
      return true;
    }
    console.log(fn([[2, 0, 0, 1], [0, 3, 1, 0], [0, 5, 2, 0], [4, 0, 0, 2]]))
  </script>
</body>
</html>